package com.webinsky.persist.dao.catalog;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.type.JdbcType;

import com.webinsky.common.utils.db.Pagnation;
import com.webinsky.persist.entity.catalog.Tag;
import com.webinsky.persist.entity.catalog.TagFilter;

public interface TagMapper {
	@Insert({
        "insert into tag (name, tag_cat_id, del, sort, language, create_at, update_at)",
        "values (#{name, jdbcType=VARCHAR}, #{tagcatId,jdbcType=INTEGER}, ",
        "#{del,jdbcType=INTEGER}, #{sort,jdbcType=INTEGER}, #{language, jdbcType=VARCHAR}, now(), now())"
    })
    @SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Integer.class)
    int insert(Tag record);
	
	@Select({
		"select id, name, tag_cat_id, del, sort, language, create_at, update_at from tag where id = #{id, jdbcType=INTEGER}"
	})
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="tag_cat_id", property="tagcatId", jdbcType=JdbcType.INTEGER),
        @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
        @Result(column="sort", property="sort", jdbcType=JdbcType.INTEGER),
        @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR),
        @Result(column="create_at", property="created", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="update_at", property="updated", jdbcType=JdbcType.TIMESTAMP)
    })
	Tag selectById(@Param("id") int id);
	
    @SelectProvider(type = TagProvider.class, method = "selectByPagnation")
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="tag_cat_id", property="tagcatId", jdbcType=JdbcType.INTEGER),
        @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
        @Result(column="sort", property="sort", jdbcType=JdbcType.INTEGER),
        @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR),
        @Result(column="create_at", property="created", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="update_at", property="updated", jdbcType=JdbcType.TIMESTAMP)
    })
    List<Tag> selectByPagnation(@Param("filter")Pagnation pagnation);
    
    @SelectProvider(type = TagProvider.class, method = "selectByTagcatIds")
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="tag_cat_id", property="tagcatId", jdbcType=JdbcType.INTEGER),
        @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
        @Result(column="sort", property="sort", jdbcType=JdbcType.INTEGER),
        @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR),
        @Result(column="create_at", property="created", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="update_at", property="updated", jdbcType=JdbcType.TIMESTAMP)
    })
    List<Tag> selectByTagcatIds(@Param("tagcatIds") List<Integer> tagcatIds);
    
    @SelectProvider(type = TagProvider.class, method = "selectByIds")
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="tag_cat_id", property="tagcatId", jdbcType=JdbcType.INTEGER),
        @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
        @Result(column="sort", property="sort", jdbcType=JdbcType.INTEGER),
        @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR),
        @Result(column="create_at", property="created", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="update_at", property="updated", jdbcType=JdbcType.TIMESTAMP)
    })
    List<Tag> selectByIds(@Param("ids")List<Integer> ids);
    
    @Select({
    	"select id, name, tag_cat_id, del, sort, language, create_at, update_at from tag where tag_cat_id = #{tagcatId, jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="tag_cat_id", property="tagcatId", jdbcType=JdbcType.INTEGER),
        @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
        @Result(column="sort", property="sort", jdbcType=JdbcType.INTEGER),
        @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR),
        @Result(column="create_at", property="created", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="update_at", property="updated", jdbcType=JdbcType.TIMESTAMP)
    })
    List<Tag> selectByTagcat(@Param("tagcatId")int tagcatId);
    
    @SelectProvider(type = TagProvider.class, method = "selectByFilter")
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="tag_cat_id", property="tagcatId", jdbcType=JdbcType.INTEGER),
        @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
        @Result(column="sort", property="sort", jdbcType=JdbcType.INTEGER),
        @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR),
        @Result(column="create_at", property="created", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="update_at", property="updated", jdbcType=JdbcType.TIMESTAMP)
    })
    List<Tag> selectByFilter(@Param("filter")TagFilter filter);
    
    @SelectProvider(type = TagProvider.class, method = "selectByTagcatFilter")
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="tag_cat_id", property="tagcatId", jdbcType=JdbcType.INTEGER),
        @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
        @Result(column="sort", property="sort", jdbcType=JdbcType.INTEGER),
        @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR),
        @Result(column="create_at", property="created", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="update_at", property="updated", jdbcType=JdbcType.TIMESTAMP)
    })
    List<Tag> selectByTagcatFilter(@Param("filter")TagFilter filter);
    
    @SelectProvider(type = TagProvider.class, method = "selectCountByFilter")
    int selectCountByFilter(@Param("filter")TagFilter filter);
    
    @SelectProvider(type = TagProvider.class, method = "selectCountByTagcatFilter")
    int selectCountByTagcatFilter(@Param("filter")TagFilter filter);
    
    @Select({
        "select",
        "count(*)",
        "from tagcat"
    })
    int selectCount();
    
    @Update({
        "update tag",
        "set tag_cat_id = #{tagcatId,jdbcType=INTEGER},",
        "name = #{name,jdbcType=VARCHAR},",
        "sort = #{sort,jdbcType=INTEGER},",
        "language = #{language, jdbcType=VARCHAR},",
        "del = #{del,jdbcType=INTEGER},",
        "create_at = #{created,jdbcType=TIMESTAMP},",
        "update_at = #{updated,jdbcType=TIMESTAMP}",
        "where Id = #{id,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(Tag record);    
}
